<?php

namespace humhub\modules\api\models;

use Yii;
use humhub\modules\post\models\Post;
use humhub\modules\content\components\ContentAddonActiveRecord;


class TaskJob extends ContentAddonActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'task_job';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
            array(['created_by', 'updated_by'], 'integer'),
            array(['message', 'created_at', 'updated_at'], 'safe'),
        );
    }

    /**
     * Count number taskjobs for this target object
     *
     * @param type $model
     * @param type $id
     * @return type
     */
    //public static function GetJobCount($model, $id)
    public static function GetJobCount($where)
    {
        $cacheID = sprintf("taskjobCount_%s", md5(json_encode($where)));
        $commentCount = Yii::$app->cache->get($cacheID);

        if ($commentCount === false) {
            $commentCount = TaskJob::find()->where($where)->count();
            Yii::$app->cache->set($cacheID, $commentCount, Yii::$app->settings->get('cache.expireTime'));
        }

        return $commentCount;
    }

    //子任务
    //$x=\humhub\modules\api\models\TaskJob::getJobs($content->getPrimaryKey(), '27');
    public function getJobs($postid, $ids=''){
        $where = [
            'object_model' => 'humhub\modules\post\models\Post',
            'object_id' => $postid,
        ];

        $idArr = explode(',', $ids);
        $idNew = [];
        foreach($idArr AS $id){
            if( is_numeric($id) && $id>0 ){
                $idNew[] = $id;
            }
        }
        if( $idNew ){
            $ids = implode(',', $idNew);
        }else{
            return [];
        }

        $query = TaskJob::find();
        $query->orderBy('created_at DESC');
        $query->where($where);
        $query->andWhere("id IN(". $ids .")");
        $comments = $query->all();
        //echo $query->createCommand()->getRawSql();exit;

        $output = array();
        foreach ($comments as $comment) {
            $user['displayname'] = $comment->user->displayName;
            $user['guid'] = $comment->user->guid;
            $user['image'] = $comment->user->getProfileImage()->getUrl();

            $_cm = $comment->attributes;
            $cm['id'] = $_cm['id'];
            $cm['message'] = $_cm['message'];
            $cm['object_id'] = $_cm['object_id'];
            $cm['created_at'] = $_cm['created_at'];
            $cm['updated_at'] = $_cm['updated_at'];
            $cm['user'] = $user;
            //$cm['canWrite'] = $comment->canWrite();
            //$cm['canDelete'] = $comment->canDelete();

            //Files
            if ($comment->isNewRecord && $comment->getRelation('content', false) !== null) {
                $files = \humhub\modules\file\models\File::findAll(['guid' => array_map('trim', explode(',', $comment->content->attachFileGuidsAfterSave))]);
            } else {
                $files = \humhub\modules\file\models\File::getFilesOfObject($comment);
            }
            /*
            $cm['image'] = [];
            foreach ($files as $file){
                if ($file->getMimeBaseType() == "image"){
                    $cm['image'][] = $file->getPreviewImageUrl(200, 200);
                }
            }
             */
            $cm['files'] = [];
            foreach ($files as $file){
                //if ($file->getMimeBaseType() == "image")
                //    continue;
                $f['url'] = $file->getUrl();
                $f['name'] = $file->file_name;
                $f['size'] = $file->size; //Yii::$app->formatter->asSize($file->size);
                $cm['files'][] = $f;
            }

            $output[] = $cm;
        }
        return $output;
    }
}
